﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using PJTY.dal;

namespace pingjia.bll
{
   public  class ConsumerService
    {
        DataClasses1DataContext db = new DataClasses1DataContext();

        /// <summary>
        /// 检查输入的用户名和密码是否正确
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>若用户名和密码正确则返回用户Id，否则返回0</returns>
        
        public int CheckLogin(string name, string password)
        {
            
            //通过数据访问层中的Consumer类查询输入的用户名和密码是否正确，若正确则返回相应的用户对象，否则返回null
            Consumer Consumer = (from c in db.Consumer
                                 where c.ConsumerName == name && c.Password == password
                                 select c).FirstOrDefault();
            if (Consumer != null)  // 用户名和密码正确
            {
                return Consumer.ConsumerID;
            }
            else  // 用户名或密码错误
            {
                return 0;
            }
        }

        /// <summary>
        /// 修改用户Id对应用户的密码
        /// </summary>
        /// <param name="ConsumerID">用户Id</param>
        /// <param name="password">新密码</param>
        public void ChangePassword(int ConsumerID, string password)
        {
            Consumer Consumer = (from c in db.Consumer
                                 where c.ConsumerID == ConsumerID
                                 select c).First();
            Consumer.Password = password;

            db.SubmitChanges();
        }

        public Consumer GetConsumerInformation(string username)//获取消费者信息
        {
            using (var db = new DataClasses1DataContext())
            {
                return db.Consumer.FirstOrDefault(c => c.ConsumerName == username);
            }
        }

        public List<Product1> GetAllProducts()
        {
            using (var db = new DataClasses1DataContext())
            {
                return db.Product1.ToList();
            }
        }

        public List<Product1> GetProductsByKeyword(string keyword)
        {
            using (var db = new DataClasses1DataContext())
            {
                return db.Product1.Where(p => p.ProductName.Contains(keyword)).ToList();
            }
        }

        /// <summary>
        /// 将用户密码重置为相应的用户名
        /// </summary>
        /// <param name="name">输入的用户名</param>
        /// <param name="email">输入的邮箱</param>
        public void ResetPassword(string name, string email)
        {
            Consumer Consumer = (from c in db.Consumer
                                 where c.ConsumerName == name && c.ConsumerEmail == email
                                 select c).First();
            Consumer.Password = name;
            db.SubmitChanges();
        }

        /// <summary>
        /// 判断输入的用户名是否重名
        /// </summary>
        /// <param name="name">输入的用户名</param>
        /// <returns>当用户名重名时返回true，否则返回false</returns>
        public bool IsNameExist(string name)
        {
            //通过MyPetShop.DAL数据访问层中的Consumer类查询输入的用户名是否重名，若重名则返回用户对象，否则返回null
            Consumer Consumer = (from c in db.Consumer
                                 where c.ConsumerName == name
                                 select c).FirstOrDefault();
            if (Consumer != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

       

        /// <summary>
        /// 判断Consumer表中是否存在输入的用户名和邮箱
        /// </summary>
        /// <param name="name">输入的用户名</param>
        /// <param name="email">输入的邮箱</param>
        /// <returns>当输入的用户名和邮箱存在时返回true，否则返回false</returns>
        public bool IsEmailExist(string name, string email)
        {
            Consumer Consumer = (from c in db.Consumer
                                 where c.ConsumerName == name && c.ConsumerEmail == email
                                 select c).FirstOrDefault();
            if (Consumer != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 向MyPetShop数据库中的Consumer表插入新用户记录
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="email">电子邮件地址</param>
        public void Insert(string name, string password, string email,int age,string iphone)
        {
            Consumer Consumer = new Consumer
            {
                ConsumerName = name,
                Password = password,
                ConsumerEmail = email,
                ConsumerAge=age,
                ConsumerPhone=iphone
            };
            db.Consumer.InsertOnSubmit(Consumer);
            db.SubmitChanges();
        }
        public void resume( int consumerID,string consumerName,int consumerAge,int productID,string productName,string trialStatus,DateTime time)//提交用户信息
        {
            Trial uss = new Trial()
            {
                ConsumerID = consumerID,
                ConsumerName = consumerName,
                ConsumerAge=consumerAge,
                ProductID = productID,
                ProductName = productName,
                TrialStatus = trialStatus,
                TrialTime = time
            };
            db.Trial.InsertOnSubmit(uss);
            db.SubmitChanges();

        }
        public void SaveImagePath(int reviewId, string imagePath)
        { 
            SqlConnection conn = new SqlConnection(); 
            string sql = "UPDATE Review SET Reviewpicture = @Reviewpicture WHERE ReviewID = @ReviewID"; 
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@Reviewpicture", imagePath);
            cmd.Parameters.AddWithValue("@ReviewID", reviewId); 
            //打开连接
            conn.Open(); 
            //执行SQL命令
            cmd.ExecuteNonQuery(); 
            //关闭连接
            conn.Close();
        }
        
        public void Review(int trialID, int consumerID, string consumerName,string productName,int consumerAge,string reviewContent,int reviewScore,string reviewnr)
        {
            Review ass = new Review()
            {
                TrialID = trialID,
                ConsumerID = consumerID,
                ConsumerName = consumerName,
                ProductName = productName,
                ConsumerAge = consumerAge,
                ReviewContent = reviewContent,
                ReviewScore = reviewScore,
                Reviewnr = reviewnr
              
            };
            db.Review.InsertOnSubmit(ass);
            db.SubmitChanges();
        }
        public void Update(string id)
        {
            Trial trial = (from t in db.Trial where t.TrialID == Convert.ToInt32(id) select t).FirstOrDefault();
            if (trial != null)
            {
                trial.TrialStatus = "审核通过";
                db.SubmitChanges();
            }
        }
        public void Update1(string id)
        {
            Trial trial = (from t in db.Trial where t.TrialID == Convert.ToInt32(id) select t).FirstOrDefault();
            if (trial != null)
            {
                trial.TrialStatus = "已发货";
                db.SubmitChanges();
            }
        }

      

        public Product1 GetProductById(int productId)//获取产品信息
        {
            using (var db = new DataClasses1DataContext())
            {
                return db.Product1.FirstOrDefault(p => p.ProductID == productId);
            }
        }
        public void ToggleCheckBoxes(List<GridViewRowData> gridViewData, bool isChecked)
        {
            foreach (var rowData in gridViewData)
            {
                rowData.IsChecked = isChecked;
            }
        }

        public List<string> GetSelectedProductIds(List<GridViewRowData> gridViewData)
        {
            List<string> selectedProductIds = new List<string>();
            foreach (var rowData in gridViewData)
            {
                if (rowData.IsChecked)
                {
                    selectedProductIds.Add(rowData.ProductId);
                }
            }
            return selectedProductIds;
        }

        public string GetDeleteConfirmationMessage(string productName)
        {
            return string.Format("您真的要删除商品名为 {0} 的记录吗?", productName);
        }
    }

    public class GridViewRowData
    {
        public string ProductId { get; set; }
        public string ProductName { get; set; }
        public bool IsChecked { get; set; }
    }




}
